package com.sniff.atp.modules.test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sniff.atp.common.api.CommonPage;
import com.sniff.atp.common.api.CommonResult;
import com.sniff.atp.modules.test.model.BugRegister;
import com.sniff.atp.modules.test.service.BugRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 线上问题登记表 前端控制器
 * </p>
 *
 * @author macro
 * @since 2023-08-13
 */
@RestController
@RequestMapping("/test/bugRegister")
@Api(value = "线上问题登记", tags = "线上问题登记")
public class BugRegisterController {

    @Autowired
    private BugRegisterService registerService;

    @ApiOperation("新增问题")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody BugRegister bugRegister) {
        boolean success = registerService.create(bugRegister);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改问题")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(
                              @RequestBody BugRegister bugRegister) {

        boolean success = registerService.update(bugRegister.getBugId(), bugRegister);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取问题详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BugRegister> getItem(@PathVariable Long id) {
        BugRegister bugRegister = registerService.getById(id);
        return CommonResult.success(bugRegister);
    }

    @ApiOperation("根据ID删除问题列表")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = registerService.removeById(id);

        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询问题")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<BugRegister>> list(@RequestBody BugRegister bugRegister) {
        if(bugRegister.getPageSize() == null){
            bugRegister.setPageSize(10);
        }
        if(bugRegister.getPageNum() == null){
            bugRegister.setPageNum(1);
        }
        Page<BugRegister> menuList = registerService.list(bugRegister, bugRegister.getPageSize(), bugRegister.getPageNum());
        return CommonResult.success(CommonPage.restPage(menuList));
    }


}

